package com.qs.cwams.service.impl;


import com.qs.common.framework.dto.QsResult;
import com.qs.common.framework.util.ConvertUtil;
import com.qs.cwams.mapper.AtteConfigMapper;
import com.qs.cwams.mapper.AtteUserMapper;
import com.qs.cwams.pojo.domain.AtteConfigDO;
import com.qs.cwams.pojo.domain.AtteUserDO;
import com.qs.cwams.pojo.view.*;
import com.qs.cwams.service.AtteConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author suncheng
 */
@Service
public class AtteConfigServiceImpl implements AtteConfigService {

    @Autowired
    private AtteConfigMapper atteConfigMapper;
    @Autowired
    private AtteUserMapper atteUserMapper;

    @Override
    public QsResult queryConfig() {
        List<AtteConfigDO> configs = atteConfigMapper.selectList(null);
        return QsResult.ok().setResult(ConvertUtil.listCopy(configs, AtteConfigVO.class));
    }

    @Override
    public QsResult setConfig(AtteConfigVO atteConfigVO) {
        AtteConfigDO atteConfigDO = new AtteConfigDO();
        atteConfigDO.setId(atteConfigVO.getId());
        atteConfigDO.setConfigValue(atteConfigVO.getConfigValue());
        atteConfigDO.setUpdateTime(new Date());
        atteConfigMapper.updateById(atteConfigDO);
        return QsResult.ok();
    }

    @Override
    public QsResult queryUser() {
        List<AtteUserDO> users = atteUserMapper.selectList(null);
        return QsResult.ok().setResult(ConvertUtil.listCopy(users, AtteUserVO.class));
    }

    @Override
    public QsResult setUser(List<AtteUserVO> users) {
        atteUserMapper.delete(null);
        users.forEach(user -> atteUserMapper.insert(ConvertUtil.copy(user, AtteUserDO.class)));
        return QsResult.ok().setResult(ConvertUtil.listCopy(users, AtteUserVO.class));
    }
}
